package com.sw.bbs.board.service.impl;

import com.sw.bbs.board.dao.BoardMapper;
import com.sw.bbs.board.dao.CommentMapper;
import com.sw.bbs.board.dao.PostMapper;
import com.sw.bbs.board.dao.user.*;
import com.sw.bbs.board.pojo.Board;
import com.sw.bbs.board.pojo.Comment;
import com.sw.bbs.board.pojo.Post;
import com.sw.bbs.board.pojo.user.UserBoardOperation;
import com.sw.bbs.board.pojo.user.UserCommentOperation;
import com.sw.bbs.board.pojo.user.UserPostOperation;
import com.sw.bbs.board.service.ProfileOperaService;
import com.sw.bbs.board.vo.request.board.OperaBoardRequest;
import com.sw.bbs.board.vo.request.comment.OperaCommentRequest;
import com.sw.bbs.board.vo.request.post.OperaPostRequest;
import com.sw.bbs.common.common.ErrorCode;
import com.sw.bbs.common.common.ServiceException;
import com.sw.bbs.user.pojo.User;
import com.sw.bbs.user.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;

@Service
@Slf4j
public class ProfileOperaServiceImpl implements ProfileOperaService {

    @Autowired
    private BoardMapper boardMapper;

    @Autowired
    private UserBoardMapper userboardMapper;

    @Autowired
    private UserBoardOperationMapper userBoardOperationMapper;

    @Autowired
    private PostMapper postMapper;

    @Autowired
    private UserPostMapper userPostMapper;

    @Autowired
    private UserPostOperationMapper userPostOperationMapper;

    @Autowired
    private CommentMapper commentMapper;

    @Autowired
    private UserCommentMapper userCommentMapper;

    @Autowired
    private UserCommentOperationMapper userCommentOperationMapper;

    @Autowired
    private UserService userService;
    /**
     * 对板块的操作
     *
     * @param request
     * @param httpServletRequest
     * @return
     */
    @Override
    @Transactional(rollbackFor = ServiceException.class)
    public Integer operaBoard(OperaBoardRequest request, HttpServletRequest httpServletRequest)  throws ServiceException{

        User user = userService.getUserByRequest(httpServletRequest);
        UserBoardOperation operation = new UserBoardOperation();

        //收藏/订阅
        if (request.getCollect() != null) {
            operation.setOperation(request.getCollect() == 1 ? 1 : 0);
            operation.setType(3);
        }

        try {
            operation.setUserId(user.getUserId());
            operation.setBoardId(request.getBoardId());
            if (userBoardOperationMapper.selectByPrimaryKey(operation)!=null) {
                userBoardOperationMapper.updateByPrimaryKeySelective(operation);
            }else  {
                userBoardOperationMapper.insertSelective(operation);
            }

            Board board = boardMapper.selectByPrimaryKey(request.getBoardId());
            if (request.getCollect() != null) {
                board.setCollectNum(request.getCollect() == 1 ? board.getCollectNum() + 1 : board.getCollectNum() - 1);
                if (board.getCollectNum() < 0 ) {
                    board.setCollectNum(0);
                }
            }
            boardMapper.updateByPrimaryKeySelective(board);
        }catch (Exception e) {
            log.error("userBoardOperation db fail");
            throw new ServiceException(ErrorCode.BUSINESS_SERVER_ERROR,"userBoardOperation db fail");

        }
        return 1;
    }
    /**
     * 对帖子的操作
     *
     * @param request
     * @param httpServletRequest
     * @return
     */
    @Override
    @Transactional(rollbackFor = ServiceException.class)
    public Integer operaPost(OperaPostRequest request, HttpServletRequest httpServletRequest)  throws ServiceException{

        User user = userService.getUserByRequest(httpServletRequest);
        UserPostOperation operation = new UserPostOperation();
        //喜欢
        if (request.getLike() != null) {
            operation.setOperation(request.getLike() == 1 ? 1 : 0);
            operation.setType(1);
        }
        //不喜欢
        else if (request.getDislike() != null) {
            operation.setOperation(request.getDislike() == 1 ? 1 : 0);
            operation.setType(2);
        }
        //收藏
        else if (request.getCollect() != null) {
            operation.setOperation(request.getCollect() == 1 ? 1 : 0);
            operation.setType(3);
        }
        //g关注
        else if (request.getFollow() != null) {
            operation.setOperation(request.getFollow() == 1 ? 1 : 0);
            operation.setType(4);
        }

        try {
            operation.setPostId(request.getPostId());
            operation.setUserId(user.getUserId());
            if (userPostOperationMapper.selectByPrimaryKey(operation)!=null) {
                userPostOperationMapper.updateByPrimaryKeySelective(operation);
            }else  {
                userPostOperationMapper.insertSelective(operation);
            }

            Post post = postMapper.selectByPrimaryKey(request.getPostId());
            post.setPostId(request.getPostId());
            if (request.getLike()!=null) {
                post.setLikeNum(request.getLike() == 1 ? post.getLikeNum()+1 : post.getLikeNum()-1);
                if (post.getLikeNum()<0){post.setLikeNum(0);}
            } else if (request.getDislike() != null) {
                post.setDislikeNum(request.getDislike() == 1 ? post.getDislikeNum() + 1 : post.getDislikeNum() - 1);
                if (post.getDislikeNum()<0){post.setDislikeNum(0);}
            }else if (request.getCollect() != null) {
                post.setCollectNum(request.getCollect() == 1 ? post.getCollectNum() + 1 : post.getCollectNum() - 1);
                if (post.getCollectNum()<0){post.setCollectNum(0);}
            }
            postMapper.updateByPrimaryKeySelective(post);
        }catch (Exception e) {
            log.error("user-post-operation db fail");
            throw new ServiceException(ErrorCode.BUSINESS_SERVER_ERROR,"user-post-operation");

        }
        return 1;
    }

    /**
     * 对评论的操作
     *
     * @param request
     * @param httpServletRequest
     * @return
     */
    @Override
    @Transactional(rollbackFor = ServiceException.class)
    public Integer operaComment(OperaCommentRequest request, HttpServletRequest httpServletRequest) throws ServiceException {

        User user = userService.getUserByRequest(httpServletRequest);
        UserCommentOperation operation = new UserCommentOperation();
        //喜欢
        if (request.getLike() != null) {
            operation.setOperation(request.getLike() == 1 ? 1 : 0);
            operation.setType(1);
        }
        //不喜欢
        else if (request.getDislike() != null) {
            operation.setOperation(request.getDislike() == 1 ? 1 : 0);
            operation.setType(2);
        }

        try {
            operation.setCommentId(request.getCommentId());
            operation.setUserId(user.getUserId());
            if (userCommentOperationMapper.selectByPrimaryKey(operation)!=null) {
                userCommentOperationMapper.updateByPrimaryKeySelective(operation);
            }else  {
                userCommentOperationMapper.insert(operation);
            }

            Comment comment = commentMapper.selectByPrimaryKey(request.getCommentId());
            if (request.getLike()!=null) {
                comment.setLikeNum(request.getLike()==1?comment.getLikeNum()+1:comment.getLikeNum()-1);
                if (comment.getLikeNum()<0){comment.setLikeNum(0);}
            } else if (request.getDislike() != null) {
                comment.setDislikeNum(request.getDislike() == 1 ? comment.getDislikeNum() + 1 : comment.getDislikeNum() - 1);
                if (comment.getDislikeNum()<0){comment.setDislikeNum(0);}
            }
            commentMapper.updateByPrimaryKeySelective(comment);

        }catch (Exception e) {
            log.error("user-post-operation db fail");
            throw new ServiceException(ErrorCode.BUSINESS_SERVER_ERROR,"user-post-operation");

        }
        return 1;
    }
}
